package com.orifine.web.validate;

import com.orifine.web.Domain;
import com.orifine.web.Property;
import com.orifine.web.Validator;

public class FormatRule {
	public static Validator email(){
		return pattern("validate.format.email", "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
	}
	
	public static Validator ip(){
		return pattern("validate.format.ip", "(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)");
	}
	
	public static Validator pattern(final String mode){
		return pattern("validate.format.pattern", mode);
	}
	
	public static Validator pattern(final String domain, final String mode){
		return new Validator() {
			@Override
			public boolean execute(Object... values) {
				if(values[0] == null){
					return false;
				}
				
				String value = values[0].toString();
				
				return value.matches(value);
			}

			@Override
			public Property property() {
				return new Property(new Domain(domain), getDomain(), mode);
			}
		};
	}
}
